//
//  AbleBusLineViewController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/2.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "AbleBusLineViewController.h"
#import "BusLineCell.h"
#import "RegularBusLineViewModel.h"
#import "RegularBusLineModel.h"
#import "RoutePlanningViewController.h"
#import "BusLineViewController.h"
#import "AbleLineRequestModel.h"
#import "PoisModel.h"


@interface AbleBusLineViewController ()
@property (strong, nonatomic) IBOutlet UITableView *mainTable;

@property (strong, nonatomic) RegularBusLineViewModel *regularBusLineViewModel;


@end

@implementation AbleBusLineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.mainTable.backgroundColor = GLOBLE_BACKGROUND_COLOR;
    [self setExtraCellLineHidden:self.mainTable];
    self.mainTable.alpha = 0;
    
    self.regularBusLineViewModel = [[RegularBusLineViewModel alloc ] init];
    [self loadNewData];
    
    // 头部刷新
    WEAKSELF
    [self addHeader:^(MJRefreshBaseView *refreshView) {
         [weakSelf loadNewData];
    } tableView:self.mainTable];
    
}

-(void)loadNewData{
    self.regularBusLineViewModel.pageIndex = 1;
    [self loadData];
}

-(void)loadMoreData{
    self.regularBusLineViewModel.pageIndex ++;
    [self loadData];
}


-(void)loadData{

    self.regularBusLineViewModel.requestLineModel.StartLat=@(self.busLineViewController.startPoisModel.latitude).stringValue;
    self.regularBusLineViewModel.requestLineModel.StartLng=@(self.busLineViewController.startPoisModel.longitude).stringValue;
    self.regularBusLineViewModel.requestLineModel.EndLat  =@(self.busLineViewController.endPoisModel.latitude).stringValue;
    self.regularBusLineViewModel.requestLineModel.EndLng  =@(self.busLineViewController.endPoisModel.longitude).stringValue;
    WEAKSELF
    [self.regularBusLineViewModel getBusLinesResultBlock:^(NSInteger resultType, id data, NSString *errorMsg) {
        [weakSelf.header endRefreshing];
        weakSelf.mainTable.alpha = 1;
        [weakSelf.mainTable reloadData];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RegularBusLineModel *model = self.regularBusLineViewModel.busLines[indexPath.row];
    RoutePlanningViewController *routePlanningViewController = (RoutePlanningViewController *)[RoutePlanningViewController getViewControllerFromSbName:@"RoutePlanning" viewControllerName:@"RoutePlanningViewController"];
    routePlanningViewController.routePlanningStatus = RoutePlanningSuccess;
    routePlanningViewController.lineId = @(model.LineId).stringValue;
    [self.busLineViewController.navigationController pushViewController:routePlanningViewController animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.regularBusLineViewModel.busLines.count > indexPath.row) {
        return 80;
    }
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.regularBusLineViewModel.isLoadMore) {
        return self.regularBusLineViewModel.busLines.count + 1;
    }
    return self.regularBusLineViewModel.busLines.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (self.regularBusLineViewModel.busLines.count > indexPath.row) {
        BusLineCell *lineCell = [tableView dequeueReusableCellWithIdentifier:[BusLineCell cellIdentifier]];
        if (lineCell == nil) {
            [tableView registerNib:[UINib nibWithNibName:[BusLineCell cellIdentifier] bundle:nil] forCellReuseIdentifier:[BusLineCell cellIdentifier]];
            lineCell = [tableView dequeueReusableCellWithIdentifier:[BusLineCell cellIdentifier]];
        }
        [lineCell showModel:self.regularBusLineViewModel.busLines[indexPath.row]];
        return lineCell;
    }else{
        if (self.regularBusLineViewModel.isLoadMore){
            self.regularBusLineViewModel.isLoadMore = NO;
            [self loadMoreData];
        }
        return self.loadingCell;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
